# -*- coding: utf-8 -*-
"""
@Time ： 2021/1/21 14:32
@Auth ： 张张呀
@File ：汽车网站.py
@IDE ：PyCharm
@Motto：ABC(Always Be Coding)

"""
import requests
import xlwt
from lxml import etree
from tqdm import tqdm


class Car:
    def __init__(self):
        self.parent_url = 'http://www.12365auto.com/'
        self.url = 'http://www.12365auto.com/ranking/index.aspx?stime=2019-01-01&etime=2021-01-01&ba=2&bt=-1&cp=0&z=0&t=4&mip=2&mxp=110&ny=0'
        self.header = {
            "user-agent": "Mozilla/5.0(Window NT 10.0 ; Win64 ; x64) AppleWebKit/537.36 (KHTML, like Gecko)/"
                          "Chrome/86.0.4240.75 Safari/537.36"
        }

    def get_url(self, sheet):
        response = requests.get(self.url, self.header).text
        tree = etree.HTML(response)
        tr_list = tree.xpath('//*[@id="content"]/div[3]/div/table//tr/td/table//tr')
        tr_list = tr_list[1:]
        tr_list.pop()
        detail_url_list = []
        row = 1
        for tr in tqdm(tr_list, desc='当前正在下载', ncols=100):
            ranking = tr.xpath('./td[1]/text()')[0]
            sheet.write(row, 0, label=ranking)
            name = tr.xpath('./td[2]/text()')[0]
            sheet.write(row, 1, label=name)
            car_series = tr.xpath('./td[3]/a/text()')[0]
            sheet.write(row, 2, label=car_series)
            motorcycle_type = tr.xpath('./td[4]/text()')[0]
            sheet.write(row, 3, label=motorcycle_type)
            brand_type = tr.xpath('./td[5]/text()')[0]
            sheet.write(row, 4, label=brand_type)
            nationality = tr.xpath('./td[6]/text()')[0]
            sheet.write(row, 5, label=nationality)
            new_url = self.parent_url + tr.xpath('./td[3]/a/@href')[0]
            sheet.write(row, 8, label=new_url)
            row += 1
            detail_url_list.append(new_url)
        return detail_url_list

    def get_xiang_qing(self, sheet, all_car_url, excel):
        new_row = 1
        for detail_url in all_car_url:
            detail_response = requests.get(detail_url, self.header)
            detail_response.encoding = 'GBK'
            detail_response = detail_response.text
            new_tree = etree.HTML(detail_response)
            guiding_price = \
                new_tree.xpath('//*[@id="content"]/div[4]/div[1]/div[1]/div[1]/div[2]/ul/li[1]/span[2]/text()')[
                    0]
            sheet.write(new_row, 6, label=guiding_price)
            speed_changing_box = \
                new_tree.xpath('//*[@id="content"]/div[4]/div[1]/div[1]/div[1]/div[2]/ul/li[7]/em/text()')[
                    0]
            sheet.write(new_row, 7, label=speed_changing_box)
            # 这里传参 四个 mi,act,seriesId,year
            # 这里地址根据车型 以及年份 自己拼装
            uu = 'http://www.12365auto.com//Server/seriesSummary.ashx?mi=0.6675075781805511&act=complaintypeMax&seriesId=30&year=2020'
            typical_problem = [tt['parentTitle'] + tt['title'] + str(tt['count']) for tt in
                               requests.get(uu, self.header).json()['data']]
            print(typical_problem)
            new_row += 1
        excel.save('./che.xls')

    def run(self):
        excel = xlwt.Workbook(encoding='ascii')
        sheet = excel.add_sheet('基本信息')
        title_info = ['排名', '品牌', '车系', '车型属性', '品牌属性', '国别', '产商指导价', '变速箱', '详情页网址']
        for i, title in enumerate(title_info):
            sheet.write(0, i, title)
        all_car_url = self.get_url(sheet)
        self.get_xiang_qing(sheet, all_car_url, excel)


if __name__ == '__main__':
    Car().run()
